package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcRefund;
import com.ruoyi.fun.service.IEcRefundService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退款/售后Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/refund")
public class EcRefundController extends BaseController
{
    @Autowired
    private IEcRefundService ecRefundService;

    /**
     * 查询退款/售后列表
     */
    @PreAuthorize("@ss.hasPermi('fun:refund:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcRefund ecRefund)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecRefund.setOrgId(orgId);
        List<EcRefund> list = ecRefundService.selectEcRefundList(ecRefund);
        return getDataTable(list);
    }

    /**
     * 导出退款/售后列表
     */
    @PreAuthorize("@ss.hasPermi('fun:refund:export')")
    @Log(title = "退款/售后", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcRefund ecRefund)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecRefund.setOrgId(orgId);
        List<EcRefund> list = ecRefundService.selectEcRefundList(ecRefund);
        ExcelUtil<EcRefund> util = new ExcelUtil<EcRefund>(EcRefund.class);
        util.exportExcel(response, list, "退款/售后数据");
    }

    /**
     * 获取退款/售后详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:refund:query')")
    @GetMapping(value = "/{refundId}")
    public AjaxResult getInfo(@PathVariable("refundId") Long refundId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecRefundService.selectEcRefundByRefundIdAndOrgId(refundId, orgId));
    }

    /**
     * 新增退款/售后
     */
    @PreAuthorize("@ss.hasPermi('fun:refund:add')")
    @Log(title = "退款/售后", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcRefund ecRefund)
    {
        ecRefund.setOrgId(getOrgId());
        return toAjax(ecRefundService.insertEcRefund(ecRefund));
    }

    /**
     * 修改退款/售后
     */
    @PreAuthorize("@ss.hasPermi('fun:refund:edit')")
    @Log(title = "退款/售后", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcRefund ecRefund)
    {
        return toAjax(ecRefundService.updateEcRefund(ecRefund));
    }

    /**
     * 删除退款/售后
     */
    @PreAuthorize("@ss.hasPermi('fun:refund:remove')")
    @Log(title = "退款/售后", businessType = BusinessType.DELETE)
	@DeleteMapping("/{refundIds}")
    public AjaxResult remove(@PathVariable Long[] refundIds)
    {
        return toAjax(ecRefundService.deleteEcRefundByRefundIds(refundIds));
    }
    /**
     * 根据refundId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:ecrefund:query')")
    @GetMapping(value = "/{refundId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("refundId") Long refundId, @PathVariable("orgId") Long orgId)
    {
        return success(ecRefundService.selectEcRefundByRefundIdAndOrgId(refundId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:ecrefund:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcRefund ecRefund = new EcRefund();
        List<EcRefund> list = ecRefundService.selectEcRefundListByOrgId(ecRefund, orgId);
        return getDataTable(list);
    }


}